/**
 * 作者：陈文斌 on 2019/1/23.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:crm/tools/apollo_util.dart';
import 'package:crm/model/Feedback/feedback_model.dart';

class FeedbackRecordVM {
  searchFeedbackRecord(
    int page,
    List<FeedbackModel> feedbackRecords, {
    String customerId,
    Function callback,
    Function errorCallback,
  }) {
    final int _take = 10; // 每次请求10条信息
    final int _skip = page * _take; // 从第skip + 1条开始请求
    bool _isMore = true;

    Map args = {
      'skip': '${_skip}',
      'take': '$_take',
    };
    if(customerId != null && customerId.isNotEmpty){
      args['customerId'] = customerId;
    }

    ApolloRequestUtil.searchFeedbackRecord(
          (response) {
        // 请求成功回调
        var _result = response['data']['feedbacks']['items'];
        if (page == 0) feedbackRecords = List<FeedbackModel>();
        for (var newsItem in _result) {
          feedbackRecords.add(FeedbackModel.fromJson(newsItem));
        }

        _isMore = _result.length == _take;

        if (callback != null) {
          callback(feedbackRecords, _isMore);
        }
      },
      args: args,
      errorCallback: (errMsg) {
        // 请求失败
        if (errorCallback != null) {
          errorCallback(errMsg);
        }
      },
    );
  }
}
